package com.lsa.letcode.the130.surroundedRegions;

import static org.junit.Assert.*;

import org.junit.Test;

import com.lsa.letcode.common.LetCodeTestBase;

public class SurroundedRegionsTest extends LetCodeTestBase {
	@Test
	public void test() throws Exception {
//		print(newBoard(
//				"X X X X \n"+
//				"X 0 0 X \n"+
//				"X X 0 X \n" +
//				"X 0 X X "));
		
//		print(newBoard(
//				"X X X X X X \n"+
//				"X X 0 0 X X \n"+
//				"X X X 0 0 X \n" +
//				"X X X X X X "));
		
		test(
				newBoard(
				"X \n"),
				newBoard(
				"X \n")
			);
		test(
				newBoard(
				"0 \n"),
				newBoard(
				"0 \n")
			);		
		
		test(
				newBoard(
				"X X X X \n"+
				"X X X X \n"+
				"X X X X \n" +
				"X 0 X X "), 
				newBoard(
				"X X X X \n"+
				"X 0 0 X \n"+
				"X X 0 X \n" +
				"X 0 X X ")
			);
		test(
				newBoard(
				"X X X X \n"+
				"X 0 0 X \n"+
				"X 0 0 X \n" +
				"X 0 X X "), 
				newBoard(
				"X X X X \n"+
				"X 0 0 X \n"+
				"X 0 0 X \n" +
				"X 0 X X ")
			);
		test(
				newBoard(
				"X X X X \n"+
				"X X X X \n"+
				"X X X X \n" +
				"X X X X "), 
				newBoard(
				"X X X X \n"+
				"X 0 0 X \n"+
				"X 0 0 X \n" +
				"X X X X ")
			);
		test(
				newBoard(
				"X X X X \n"+
				"X X X X \n"+
				"X X X X \n" +
				"X X X X "), 
				newBoard(
				"X X X X \n"+
				"X 0 X X \n"+
				"X X X X \n" +
				"X X X X ")
			);
		test(
				newBoard(
				"X 0 X X \n"+
				"X X X X \n"+
				"X X X X \n" +
				"X X X X "), 
				newBoard(
				"X 0 X X \n"+
				"X X X X \n"+
				"X X X X \n" +
				"X X X X ")
			);
		test(
				newBoard(
				"X 0 X X \n"+
				"X 0 0 X \n"+
				"X X 0 X \n" +
				"X X X X "), 
				newBoard(
				"X 0 X X \n"+
				"X 0 0 X \n"+
				"X X 0 X \n" +
				"X X X X ")
			);
		test(
				newBoard(
				"X 0 X X \n"+
				"X 0 X X \n"+
				"X X X X \n" +
				"X X X X "), 
				newBoard(
				"X 0 X X \n"+
				"X 0 X X \n"+
				"X X 0 X \n" +
				"X X X X ")
			);
		
		test(
				newBoard(
				"X X X X X X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X "), 
				newBoard(
				"X X X X X X \n"+
				"X X 0 0 X X \n"+
				"X X X 0 0 X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X 0 X X X X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X "), 
				newBoard(
				"X 0 X X X X \n"+
				"X X 0 0 X X \n"+
				"X X X 0 0 X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X 0 X X 0 X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X "), 
				newBoard(
				"X 0 X X 0 X \n"+
				"X X 0 0 X X \n"+
				"X X X 0 0 X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X 0 X 0 0 X \n"+
				"X X 0 0 X X \n"+
				"X X X 0 0 X \n" +
				"X X X X X X "),
				newBoard(
				"X 0 X 0 0 X \n"+
				"X X 0 0 X X \n"+
				"X X X 0 0 X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X X X X X X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X "),
				newBoard(
				"X X X X X X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X X X X X X \n"+
				"X X X X X X \n"+
				"X X X X X X \n" +
				"X X X X X X "),
				newBoard(
				"X X X X X X \n"+
				"X 0 0 0 X X \n"+
				"X 0 0 0 X X \n" +
				"X X X X X X ")
			);
		
		test(
				newBoard(
				"X 0 X X X X \n"+
				"X 0 X 0 0 X \n"+
				"X 0 0 0 X X \n" +
				"X X X X X X "),
				newBoard(
				"X 0 X X X X \n"+
				"X 0 X 0 0 X \n"+
				"X 0 0 0 X X \n" +
				"X X X X X X ")
			);
	}
	
	void test(char[][] expected, char[][] in) {
		Solution s = new Solution();
		s.solve(in);
		for (int r = 0; r < in.length; r++) {
			for (int c = 0; c < in[0].length; c++) {
				if (expected[r][c] != in[r][c]) {
					System.out.println("err");
					print(in);
					assertFalse(true);
				}
			}
		}
	}
	
	char[][] newBoard(String s) {
		String[] rows = s.split("\n");
		int rCnt = rows.length;
		int cCnt = rows[0].length() / 2;
		char[][] board = new char[rCnt][cCnt];
		for (int r = 0; r < rCnt; r++) {
			for (int c = 0; c < cCnt; c++) {
				char chr = rows[r].charAt(c * 2);
				if (chr == '0') {
					chr = 'O';
				}
				board[r][c] = chr;
			}
		}
		return board;
	}
	
	void print(char[][] board) {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				System.out.print(String.format("%s ", board[i][j]));
			}
			System.out.println();
		}
	}
}
